﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.Paint.Mesh.CUiMeshWindow
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.CodeDom.Compiler;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Threading;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;
using Tecnomatix.Engineering.Ui.WPF;


namespace DnProcessSimulateCommands.Paint.Mesh;

public class CUiMeshWindow : TxWindow, IComponentConnector
{
  private CApMeshManager m_objectsManager;
  internal TxObjectGridControl m_selectedParts;
  internal System.Windows.Controls.Button m_btnPreview;
  internal System.Windows.Controls.Button m_btnOK;
  internal System.Windows.Controls.Button m_btnCancel;
  internal Image iMessageType;
  internal TextBlock tbMessage;
  private bool _contentLoaded;

  public CUiMeshWindow(TxObjectList selectedObjects)
  {
    this.InitializeComponent();
    this.InitializeDialog(selectedObjects);
  }

  private void InitializeDialog(TxObjectList selectedObjects)
  {
    this.m_objectsManager = new CApMeshManager(selectedObjects);
    this.InitSelectedParts();
    ((FrameworkElement) this).DataContext = (object) this.m_objectsManager;
    TxApplication.ActiveSelection.Clear();
  }

  private void TxWindow_Loaded(object sender, RoutedEventArgs e)
  {
    this.LoadValuesFromRegistry();
    this.m_selectedParts.Focus();
  }

  private void TxWindow_Closed(object sender, EventArgs e)
  {
    this.m_objectsManager.ResetPreviewMesh();
    if (this.m_objectsManager == null)
      return;
    this.m_objectsManager.Dispose();
    this.m_objectsManager = (CApMeshManager) null;
  }

  private void StoreValuesToRegistry()
  {
    CUiMeshUtil.StoreValuesToRegistry((TxWindow) this, this.m_objectsManager);
  }

  private void LoadValuesFromRegistry()
  {
    CUiMeshUtil.LoadValuesFromRegistry((TxWindow) this, ref this.m_objectsManager);
  }

  private void m_previewBtn_Click(object sender, RoutedEventArgs e)
  {
    this.m_objectsManager.MeshIsCreating = true;
    ((DispatcherObject) this).Dispatcher.Invoke(DispatcherPriority.Background, (Delegate) (() => { }));
    Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
    Mouse.UpdateCursor();
    this.m_objectsManager.PreviewMesh();
    Mouse.OverrideCursor = (System.Windows.Input.Cursor) null;
    Mouse.UpdateCursor();
    TxApplication.RefreshDisplay();
    this.m_objectsManager.MeshIsCreating = false;
  }

  private void m_cancelBtn_Click(object sender, RoutedEventArgs e) => ((Window) this).Close();

  private void m_okBtn_Click(object sender, RoutedEventArgs e)
  {
    this.m_objectsManager.MeshIsCreating = true;
    ((DispatcherObject) this).Dispatcher.Invoke(DispatcherPriority.Render, (Delegate) (() => { }));
    Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
    Mouse.UpdateCursor();
    this.m_objectsManager.ExecuteMesh();
    Mouse.OverrideCursor = (System.Windows.Input.Cursor) null;
    Mouse.UpdateCursor();
    TxApplication.RefreshDisplay();
    this.ShowSummary();
    this.m_objectsManager.MeshIsCreating = false;
    if (!this.m_objectsManager.NewMeshCreated)
      return;
    this.StoreValuesToRegistry();
    ((Window) this).Close();
  }

  private void ShowSummary()
  {
    string str1 = (string) null;
    for (int index = 0; index < 20 && index < ((Collection<ITxObject>) this.m_objectsManager.SelectedParts).Count; ++index)
    {
      ITxObject selectedPart = ((Collection<ITxObject>) this.m_objectsManager.SelectedParts)[index];
      string str2 = $"{str1}{MeshResource.MESH_SUMMARY_PART_TITLE} {selectedPart.Name}:" + "\n     ";
      str1 = (!CUiMeshUtil.DoesPartHaveMesh(selectedPart) ? str2 + MeshResource.MESH_SUMMARY_MESH_FAILED : str2 + MeshResource.MESH_SUMMARY_MESH_CREATEED) + "\n";
    }
    if (((Collection<ITxObject>) this.m_objectsManager.SelectedParts).Count > 20)
      str1 += ". . . .";
    int num = (int) TxMessageBox.ShowModal(str1, MeshResource.MESH_NAME + " - Results", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  }

  private void selectedParts_Changed(object sender, EventArgs args) => this.UpdateObjectsList();

  private void selectedParts_RowDeleted(object sender, TxObjGridCtrl_RowDeletedEventArgs args)
  {
    this.UpdateObjectsList();
  }

  private void selectedParts_ObjectInserted(
    object sender,
    TxObjGridCtrl_ObjectInsertedEventArgs args)
  {
    if (args.ObjectNumberInCurrentInsertion != args.TotalObjectsInCurrentInsertion)
      return;
    this.UpdateObjectsList();
  }

  private void selectedParts_DataChanged(object sender, TxObjGridCtrl_DataChangedEventArgs args)
  {
    this.UpdateObjectsList();
  }

  private void selectedParts_DataCleared(object sender, EventArgs args) => this.UpdateObjectsList();

  private void InitSelectedParts()
  {
    TxGridColumnData txGridColumnData1 = new TxGridColumnData();
    txGridColumnData1.Init(MeshResource.MESH_LIST_TITLE, (TxGridColumnType) 0);
    TxGridColumnData txGridColumnData2 = new TxGridColumnData();
    txGridColumnData2.Init(MeshResource.MESH_EXISTS_TITLE, (TxGridColumnType) 1);
    TxGridColumnDataCollection columnDataCollection = new TxGridColumnDataCollection();
    columnDataCollection.AddItem(txGridColumnData1);
    columnDataCollection.AddItem(txGridColumnData2);
    TxGridDescription txGridDescription = new TxGridDescription();
    txGridDescription.SetData(columnDataCollection, 0);
    this.m_selectedParts.Init(txGridDescription);
    this.m_selectedParts.SetValidator((ITxValidator) new CApMeshPartsValidator());
    this.m_selectedParts.ListenToPick = true;
    this.m_selectedParts.Objects = this.m_objectsManager.SelectedParts;
  }

  private void UpdateObjectsList()
  {
    this.m_objectsManager.SelectedParts = CUiMeshUtil.FindParts(this.m_selectedParts.Objects);
    for (int index = 0; index < this.m_selectedParts.Count; ++index)
    {
      ImageSource imageSource = !CUiMeshUtil.DoesPartHaveMesh(((Collection<ITxObject>) this.m_selectedParts.Objects)[index]) ? CUiMeshUtil.DashIconImage() : CUiMeshUtil.VIconImage();
      this.m_selectedParts.SetCellImage(index, 1, imageSource);
    }
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    System.Windows.Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/paint/mesh/cuimeshwindow.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        ((Window) target).Closed += new EventHandler(this.TxWindow_Closed);
        ((FrameworkElement) target).Loaded += new RoutedEventHandler(this.TxWindow_Loaded);
        break;
      case 2:
        this.m_selectedParts = (TxObjectGridControl) target;
        this.m_selectedParts.ValidObjectTyped += new TxObjGridCtrl_ValidObjectTypedEventHandler(this.selectedParts_Changed);
        this.m_selectedParts.InvalidObjectTyped += new TxObjGridCtrl_InvalidObjectTypedEventHandler(this.selectedParts_Changed);
        this.m_selectedParts.Picked += new TxObjGridCtrl_PickedEventHandler(this.selectedParts_Changed);
        this.m_selectedParts.RowDeleted += new TxObjGridCtrl_RowDeletedEventHandler(this.selectedParts_RowDeleted);
        this.m_selectedParts.ObjectInserted += new TxObjGridCtrl_ObjectInsertedEventHandler(this.selectedParts_ObjectInserted);
        this.m_selectedParts.DataChanged += new TxObjGridCtrl_DataChangedEventHandler(this.selectedParts_DataChanged);
        this.m_selectedParts.DataCleared += new TxObjGridCtrl_DataClearedEventHandler(this.selectedParts_DataCleared);
        break;
      case 3:
        this.m_btnPreview = (System.Windows.Controls.Button) target;
        this.m_btnPreview.Click += new RoutedEventHandler(this.m_previewBtn_Click);
        break;
      case 4:
        this.m_btnOK = (System.Windows.Controls.Button) target;
        this.m_btnOK.Click += new RoutedEventHandler(this.m_okBtn_Click);
        break;
      case 5:
        this.m_btnCancel = (System.Windows.Controls.Button) target;
        this.m_btnCancel.Click += new RoutedEventHandler(this.m_cancelBtn_Click);
        break;
      case 6:
        this.iMessageType = (Image) target;
        break;
      case 7:
        this.tbMessage = (TextBlock) target;
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }
}
